import type { PPTTextElement, Slide } from '@/types/slides'
import { nanoid } from 'nanoid'

const SCORE_FIXED_KEY = {
  总得分: 'tScore',
  总得分分析: 'tReport',
  要素名称: 'eName',
  要素得分: 'eScore',
  要素得分分析: 'eReport',
  痛点描述: 'pContext',
  指标名称: 'iName',
  指标得分: 'iScore',
  指标得分分析: 'iReport',
} as const

type FixedKeyType = keyof typeof SCORE_FIXED_KEY
interface IScoreElementOptions {
  left: number
  top: number
  width?: number
}

function getScoreFixedKey(key: '总得分' | '总得分分析' | '痛点描述' ): string
function getScoreFixedKey(
  key:
    | '要素名称'
    | '要素得分'
    | '要素得分分析'
    | '指标名称'
    | '指标得分'
    | '指标得分分析',
  index?: number,
): string

function getScoreFixedKey(key: FixedKeyType, index?: number): string {
  return `${SCORE_FIXED_KEY[key]}${index ?? ''}`
}

/**
 * 生成总得分标题
 */
const scoreTotalElement = ({
  left,
  top,
}: IScoreElementOptions): PPTTextElement => {
  return {
    type: 'text',
    id: nanoid(10),
    left: left,
    top: top,
    width: 360,
    height: 53,
    content: `<p style=""><strong><span style="font-size: 22px;">总得分:</span></strong> <strong><span style="color: rgba(36,179,241,1);">{{${getScoreFixedKey(
      '总得分',
    )}}}</span></strong></p>`,
    rotate: 0,
    defaultFontName: 'Microsoft Yahei',
    defaultColor: '#333',
    vertical: false,
    fill: 'rgba(255,255,255,1)',
  }
}

/**
 * 生成要素得分标题
 */
const scoreETitleElement = ({
  left,
  top,
}: IScoreElementOptions): PPTTextElement => {
  return {
    type: 'text',
    id: nanoid(10),
    left: left,
    top: top,
    width: 360,
    height: 53,
    content: `<p style=""><strong><span style="font-size: 22px;">{{${getScoreFixedKey(
      '要素名称',
    )}}}:</span></strong> <strong><span style="color: rgba(36,179,241,1);">{{${getScoreFixedKey(
      '要素得分',
    )}}}</span></strong></p>`,
    rotate: 0,
    defaultFontName: 'Microsoft Yahei',
    defaultColor: '#333',
    vertical: false,
    fill: 'rgba(255,255,255,1)',
  }
}

/**
 * 生成得分
 */
const scoreSubCountElement = (
  valueKey: string,
  labelKey: string,
  { left, top }: IScoreElementOptions,
): PPTTextElement => {
  return {
    type: 'text',
    id: nanoid(10),
    left: left,
    top: top,
    width: 180,
    height: 85,
    content: `<p style="text-align: center;"><strong><span>{{${valueKey}}}</span></strong></p><p style="text-align: center;color: rgb(36, 179, 241);">{{${labelKey}}}</p>`,
    rotate: 0,
    defaultFontName: 'Microsoft Yahei',
    defaultColor: '#333',
    vertical: false,
    lineHeight: 1.5,
  }
}

/**
 * 生成要素描述
 */
const scoreDescriptionElement = (
  valueKey: string,
  { left, top, width }: IScoreElementOptions,
): PPTTextElement => {
  return {
    type: 'text',
    id: nanoid(10),
    left: left,
    top: top,
    width: width || 904.7,
    height: 275,
    content: `<p style=""><span style="font-size: 16px;">{{${valueKey}}}</span></p>`,
    rotate: 0,
    defaultFontName: 'Microsoft Yahei',
    defaultColor: '#333',
    vertical: false,
  }
}

/**
 * 生成评分默认布局(2页)
 */
const genDefaultScoreLayout = (): Slide[] => {
  const totalPosition = scoreTotalElement({ left: 22.4, top: 12.2 })
  const totalEPosition = scoreETitleElement({ left: 22.4, top: 12.2 })

  const textPosition1 = () => {
    return [
      {
        labelKey: getScoreFixedKey(`要素名称`, 0),
        valueKey: getScoreFixedKey(`要素得分`, 0),
        left: 54.6,
        top: 65.2,
      },
      {
        labelKey: getScoreFixedKey(`要素名称`, 1),
        valueKey: getScoreFixedKey(`要素得分`, 1),
        left: 243.4,
        top: 65.2,
      },
      {
        labelKey: getScoreFixedKey(`要素名称`, 2),
        valueKey: getScoreFixedKey(`要素得分`, 2),
        left: 599.3,
        top: 65.2,
      },
      {
        labelKey: getScoreFixedKey(`要素名称`, 3),
        valueKey: getScoreFixedKey(`要素得分`, 3),
        left: 779.3,
        top: 65.2,
      },
      {
        labelKey: getScoreFixedKey(`要素名称`, 4),
        valueKey: getScoreFixedKey(`要素得分`, 4),
        left: 54.6,
        top: 153.8,
      },
      {
        labelKey: getScoreFixedKey(`要素名称`, 5),
        valueKey: getScoreFixedKey(`要素得分`, 5),
        left: 243.4,
        top: 153.8,
      },
      {
        labelKey: getScoreFixedKey(`要素名称`, 6),
        valueKey: getScoreFixedKey(`要素得分`, 6),
        left: 599.3,
        top: 153.8,
      },
      {
        labelKey: getScoreFixedKey(`要素名称`, 7),
        valueKey: getScoreFixedKey(`要素得分`, 7),
        left: 779.3,
        top: 153.8,
      },
    ]
  }
  const textPosition2 = () => {
    return [
      {
        labelKey: getScoreFixedKey(`指标名称`, 0),
        valueKey: getScoreFixedKey(`指标得分`, 0),
        left: 54.6,
        top: 65.2,
      },
      {
        labelKey: getScoreFixedKey(`指标名称`, 1),
        valueKey: getScoreFixedKey(`指标得分`, 1),
        left: 243.4,
        top: 65.2,
      },
      {
        labelKey: getScoreFixedKey(`指标名称`, 2),
        valueKey: getScoreFixedKey(`指标得分`, 2),
        left: 599.3,
        top: 65.2,
      },
      {
        labelKey: getScoreFixedKey(`指标名称`, 3),
        valueKey: getScoreFixedKey(`指标得分`, 3),
        left: 779.3,
        top: 65.2,
      },
      {
        labelKey: getScoreFixedKey(`指标名称`, 4),
        valueKey: getScoreFixedKey(`指标得分`, 4),
        left: 54.6,
        top: 153.8,
      },
      {
        labelKey: getScoreFixedKey(`指标名称`, 5),
        valueKey: getScoreFixedKey(`指标得分`, 5),
        left: 243.4,
        top: 153.8,
      },
      {
        labelKey: getScoreFixedKey(`指标名称`, 6),
        valueKey: getScoreFixedKey(`指标得分`, 6),
        left: 599.3,
        top: 153.8,
      },
      {
        labelKey: getScoreFixedKey(`指标名称`, 7),
        valueKey: getScoreFixedKey(`指标得分`, 7),
        left: 779.3,
        top: 153.8,
      },
    ]
  }

  const descPosition1 = [
    {
      valueKey: getScoreFixedKey('总得分分析'),
      left: 54.6,
      top: 238.8,
    },
  ]

  const descPosition2 = [
    {
      valueKey: getScoreFixedKey('要素得分分析'),
      left: 54.6,
      top: 238.8,
      width: 814,
    },
  ]

  const scoreGroup1 = textPosition1().map((item) =>
    scoreSubCountElement(item.valueKey, item.labelKey, item),
  )
  const scoreGroup2 = textPosition2().map((item) =>
    scoreSubCountElement(item.valueKey, item.labelKey, item),
  )
  const descGroup1 = descPosition1.map((item) =>
    scoreDescriptionElement(item.valueKey, item),
  )
  const descGroup2 = descPosition2.map((item) =>
    scoreDescriptionElement(item.valueKey, item),
  )
  const page1: Slide = {
    id: nanoid(10),
    elements: [totalPosition, ...scoreGroup1, ...descGroup1],
    background: {
      type: 'solid',
      color: '#ffffff',
    },
  }

  const page2: Slide = {
    id: nanoid(10),
    elements: [totalEPosition, ...scoreGroup2, ...descGroup2],
    background: {
      type: 'solid',
      color: '#ffffff',
    },
  }

  return [page1, page2]
}

export {
  SCORE_FIXED_KEY,
  genDefaultScoreLayout,
  getScoreFixedKey,
  scoreTotalElement,
  scoreSubCountElement,
  scoreDescriptionElement,
}
